package com.tongji.housediy.model
{
	import com.tongji.housediy.vo.Brand;
	import com.tongji.housediy.vo.Goods;
	import com.tongji.housediy.vo.GoodsSort;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.collections.XMLListCollection;
	
	import org.robotlegs.mvcs.Actor;
	
	/**
	 * 
	 * @author liaoruohuai
	 * 2011-4-2
	 */
	[Bindable]
	public class BrandModel extends Actor
	{
		public var brandNameArrayCollection:ArrayCollection;
		
		public var brandList:ArrayCollection = new ArrayCollection();
		
		public var xml:XML = new XML(brandList);

		public var brandXML:XML;

		public var brandXMLList:XMLListCollection;
                            

		public var goodsList:ArrayCollection;
		

		public var currentBrand:Brand;
		
		private var selectedIndexArray:Array;

		public var currentIndex:int = 0;
		
		public var typeXML:XMLListCollection;
				
		public var currentGoodsIndex:int = 0;
		
		public var total:int = 0;
		
		public function BrandModel()
		{
			brandNameArrayCollection = new ArrayCollection();
		}
		
		public function updateBrandList(brandList:ArrayCollection):void
		{
			this.brandList = brandList;
		}
		
		public function updateBrandXML(brandXML:XML):void
		{
			this.brandXML = brandXML;
			brandXMLList = new XMLListCollection(brandXML.children());
		}
		
		public function updateBrandModel(brandModel:ArrayCollection):void
		{	
			if (brandModel.length > 0)
			{
				brandNameArrayCollection = brandModel;
			}
			else if (brandModel.length == 0)
			{
				brandNameArrayCollection.removeAll();
			}
		}
		
		public function updateGoodsList(goodsList:ArrayCollection):void
		{
			this.goodsList = goodsList;
			var xml:XML = <root>
				              <type index ='0'>全部</type>
                        </root>;
			var i:int = 0;
			for(i; i < goodsList.length; i++)
			{
				var s:String = "<type index=''>" + (goodsList[i] as GoodsSort).typeName + "</type>";
				var typexml:XML = new XML(s);
				typexml.@index =  (goodsList[i] as GoodsSort).typeID;
				//typexml.@index =  (i+1);
				xml.appendChild(typexml);
			}
			typeXML = new XMLListCollection(xml.children());
			total = 0;
			for each(var groupSort:GoodsSort in goodsList)
			{
				total = total + groupSort.goodsList.length;
			}
		}
		
		public function filterArrayCollection(id:String, selectedIndexArray:Array):void
		{
			this.selectedIndexArray = selectedIndexArray;
			switch(id)
			{
				case "rightTypeList":
					filterOneAC(goodsList);
					break;
				case "brandList":
					filterOneAC(brandList);
					break;
			}			
		}
		
		private function filterOneAC(data:ArrayCollection):void
		{
			if ((selectedIndexArray.length == 0) || (selectedIndexArray.length == 1 && selectedIndexArray[0] == 0))
				data.filterFunction = null;
			else
				data.filterFunction = filterDataFunction;
			
			data.refresh();
		}
		
		private function filterDataFunction(item:Object):Boolean
		{
			if(item is GoodsSort)
			{
				if (selectedIndexArray.indexOf(item.typeID.toString()) != -1)
					return true;
				else
					return false;	
			}
			else
			{
				if (selectedIndexArray.indexOf(item.brandID.toString()) != -1)
					return true;
				else
					return false;	
			}
		}
		
		public function setSelectedIndex(currentBrand:Brand):void
		{
			this.currentBrand = currentBrand;
			var s:String = "<type index=''>"+ currentBrand.brandName+ "</type>";
			var xml:XML = new XML(s);
			xml.@index = currentBrand.brandID.toString();
			var i:int = 0;
			
			for(i; i < brandXMLList.length; i++)
			{
				if(brandXMLList.getItemAt(i) == xml)
				{
					currentIndex = i;
				}
			}
		}

	}
}